<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="113px">
      <el-form-item label="日期" prop="dateInfo">
        <el-date-picker clearable v-model="queryParams.dateInfo" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择日期" :default-value="getDefaultDate()">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="产品ID" prop="productId">
        <el-input v-model="queryParams.productId" placeholder="请输入产品ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="产品名称" prop="productName">
        <!-- <el-input v-model="queryParams.productName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" /> -->
        <el-select v-model="queryParams.productName" placeholder="请选择产品名称" clearable filterable>
          <el-option v-for="(item, i) in extData.productNameList || []" :key="i" :value="item"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="价格" prop="priceAmount">
        <el-input v-model="queryParams.priceAmount" placeholder="请输入价格" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="当日收益" prop="dayProfit">
        <el-input v-model="queryParams.dayProfit" placeholder="请输入当日收益" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="总uv量" prop="totalUvNumber">
        <el-input v-model="queryParams.totalUvNumber" placeholder="请输入总uv量" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="当日uv总量" prop="dayUvNumber">
        <el-input v-model="queryParams.dayUvNumber" placeholder="请输入当日uv总量" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="已经产生的uv量" prop="hadUvNumber">
        <el-input v-model="queryParams.hadUvNumber" placeholder="请输入已经产生的uv量" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="small" @click="handleAdd"
          v-hasPermi="['product:profitInfo:add']">新增</el-button>
      </el-col> -->
    <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="small" :disabled="single" @click="handleUpdate"
          v-hasPermi="['product:profitInfo:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="small" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['product:profitInfo:remove']">删除</el-button>
      </el-col> -->
    <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="small" @click="handleExport"
          v-hasPermi="['product:profitInfo:export']">导出</el-button>
      </el-col> -->
    <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="profitInfoList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="日期" align="center" prop="dateInfo" />

      <!-- <el-table-column label="产品ID" align="center" prop="productId" /> -->
      <el-table-column label="产品名称" align="center" prop="productName">
        <template slot-scope="scope">
          {{ scope.row.productName }} [{{ scope.row.productId }}]
        </template>
      </el-table-column>

      <el-table-column label="价格" align="center" prop="priceAmount" />

      <el-table-column label="联登成功数量" align="center" prop="successUvNumber" />
      <el-table-column label="uv成功数量" align="center" prop="dayUvNumber" />

      <el-table-column label="产品收益" align="center" prop="dayProfit">
<!--        <template slot-scope="scope">-->
<!--          {{ (scope.row.dayUvNumber * scope.row.priceAmount).toFixed(2) }}-->
<!--        </template>-->
      </el-table-column>

      <!-- <el-table-column label="总uv量" align="center" prop="totalUvNumber" /> -->
      <!-- <el-table-column label="已经产生的uv量" align="center" prop="hadUvNumber" /> -->

      <!-- <el-table-column label="当日收益" align="center" prop="dayProfit" /> -->

      <el-table-column label="商务信息" align="center" prop="ownerName">
        <template slot-scope="scope">
          {{ scope.row.ownerName }} [ {{ scope.row.ownerId }} ]
        </template>
      </el-table-column>
      <el-table-column label="详情" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="text"
            @click="$router.push(`/product/profitUvInfo?productId=${scope.row.productId}&dateInfo=${scope.row.dateInfo}`)">详情</el-button>
          <!-- <el-button size="small" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['product:profitInfo:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改产品收益信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="日期" prop="dateInfo">
          <el-input v-model="form.dateInfo" placeholder="请输入日期" />
        </el-form-item>
        <el-form-item label="产品ID" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入产品ID" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <!-- <el-input v-model="form.productName" placeholder="请输入产品名称" /> -->
          <el-select v-model="form.productName" placeholder="请选择产品名称" clearable>
            <el-option v-for="(item, i) in extData.productNameList || []" :key="i" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="价格" prop="priceAmount">
          <el-input v-model="form.priceAmount" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="总uv量" prop="totalUvNumber">
          <el-input v-model="form.totalUvNumber" placeholder="请输入总uv量" />
        </el-form-item>
        <el-form-item label="当日uv总量" prop="dayUvNumber">
          <el-input v-model="form.dayUvNumber" placeholder="请输入当日uv总量" />
        </el-form-item>
        <el-form-item label="已经产生的uv量" prop="hadUvNumber">
          <el-input v-model="form.hadUvNumber" placeholder="请输入已经产生的uv量" />
        </el-form-item>
        <el-form-item label="当日收益" prop="dayProfit">
          <el-input v-model="form.dayProfit" placeholder="请输入当日收益" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProfitInfo, getProfitInfo, delProfitInfo, addProfitInfo, updateProfitInfo, baseInfoExtData } from "@/api/product/profitInfo";

export default {
  name: "ProfitInfo",
  data() {
    return {
      extData: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品收益信息表格数据
      profitInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dateInfo: this.getDefaultDate(),
        dateInfo: null,
        productId: null,
        productName: null,
        priceAmount: null,
        totalUvNumber: null,
        dayUvNumber: null,
        hadUvNumber: null,
        dayProfit: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dateInfo: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        productId: [
          { required: true, message: "产品ID不能为空", trigger: "blur" }
        ],
        productName: [
          { required: true, message: "产品名称不能为空", trigger: "blur" }
        ],
        priceAmount: [
          { required: true, message: "价格不能为空", trigger: "blur" }
        ],
        totalUvNumber: [
          { required: true, message: "总uv量不能为空", trigger: "blur" }
        ],
        dayUvNumber: [
          { required: true, message: "当日uv总量不能为空", trigger: "blur" }
        ],
        hadUvNumber: [
          { required: true, message: "已经产生的uv量不能为空", trigger: "blur" }
        ],
        dayProfit: [
          { required: true, message: "当日收益不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getOptions();
  },
  methods: {
    getOptions() {
      baseInfoExtData().then((response) => {
        this.extData = response.data;
      });
    },
    /** 查询产品收益信息列表 */
    getList() {
      this.loading = true;
      listProfitInfo(this.queryParams).then(response => {
        this.profitInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getDefaultDate() {
      // 获取当天日期，并以 yyyy-MM-dd 格式返回
      const today = new Date();
      const year = today.getFullYear();
      const month = String(today.getMonth() + 1).padStart(2, '0');
      const day = String(today.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dateInfo: null,
        productId: null,
        productName: null,
        priceAmount: null,
        totalUvNumber: null,
        dayUvNumber: null,
        hadUvNumber: null,
        dayProfit: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品收益信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProfitInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品收益信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProfitInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProfitInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除产品收益信息编号为"' + ids + '"的数据项？').then(function () {
        return delProfitInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('product/profitInfo/export', {
        ...this.queryParams
      }, `profitInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
